import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class MyBank {

    int[][] need;
    int[][] allocation;
    int[] available;
    int X;
    int Y;
    ArrayList<Integer> waitList = new ArrayList<>();//等待序列
    ArrayList<Integer> doneList = new ArrayList<>();//处理好的序列

    public MyBank(int x, int y) {
        X = x;
        Y = y;
        need = new int[x][y];
        allocation = new int[x][y];
        available = new int[y];
        for (int i = 0; i < X; i++) {
            waitList.add(i);
        }
    }

    public void read(String path, int[][] ints) throws Exception {
        FileReader reader = new FileReader(path);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String data;
        int j = 0;
        while ((data = bufferedReader.readLine()) != null) {
            String[] str = data.split(" ");
            for (int i = 0; i < str.length; i++) {
                ints[j][i] = Integer.parseInt(str[i]);
            }
            j++;
        }
        bufferedReader.close();
    }

    public void read2(String path, int[] ints) throws Exception {
        FileReader reader = new FileReader(path);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String data;
        while ((data = bufferedReader.readLine()) != null) {
            String[] str = data.split(" ");
            for (int i = 0; i < str.length; i++) {
                ints[i] = Integer.parseInt(str[i]);
            }
        }
        bufferedReader.close();
    }

    public boolean isEnough(int[] ints_1, int[] ints_2) {
        for (int i = 0; i < ints_1.length; ++i) {
            if (ints_1[i] > ints_2[i]) {
                return false;
            }
        }
        return true;
    }

    public int[] plus(int[] ints_1, int[] ints_2) {
        int[] newInts = new int[ints_1.length];
        for (int i = 0; i < ints_1.length; i++) {
            newInts[i] = ints_1[i] + ints_2[i];
        }
        return newInts;
    }

    public void algorithm() {
        while (waitList.size() != 0) {
            for (int i = 0; i < waitList.size(); i++) {
                if (isEnough(need[waitList.get(i)], available)) {
                    available = plus(allocation[waitList.get(i)], available);
                    doneList.add(waitList.get(i));
                    waitList.remove(i);
                    i = 0;
                }
            }
        }
    }

    public void output() throws Exception {
        algorithm();
        read2(Main.AVAILABLE_PATH, available);
        for (var i : doneList) {
            System.out.print("P" + i + ":   ");
            for (var j : available)
                System.out.print(j + "  ");
            System.out.print("  ");

            for (var j : need[i])
                System.out.print(j + "  ");
            System.out.print("  ");

            for (var j : allocation[i])
                System.out.print(j + "  ");
            System.out.print("  ");

            for (int j = 0; j < need[i].length; j++) {
                available[j] = available[j] + allocation[i][j];
                System.out.print(available[j] + " ");
            }
            System.out.println();

        }
    }
}
